<?php 
namespace app\single\api;

use app\single\model\LogLoginModel;
use app\single\model\RoleModel;
use Exception;
use think\Db;
use think\facade\Cache;
use think\facade\Log;

class LogTower
{
    public function __construct()
    {
    }

    public function run($params)
    {
        $data["ret"] = 1;
        $maxValue = $params["max_value"] ?? 0;//最大值
        $limit = $params["limit"] ?? 50;

        $table = config("database.log") . ".log_login"  ;
        $roleTable = config("database.log") . ".role"  ;

        try {
            $sql="SELECT l.id, l.role_id, r.name, r.charge,r.fc, p.rmb as charge, MAX(l.tower_id) AS max_tower_id FROM {$table} l
LEFT JOIN {$roleTable} r ON l.role_id = r.id
LEFT JOIN role_pay_total p ON l.role_id = p.role_id
GROUP BY l.role_id
ORDER BY max_tower_id DESC
LIMIT 50;";
            // Log::write("msg query  {$sql}");
            $LogData = Db::query($sql);
            // Log::write("debug logdata " .json_encode($LogData,true));
            // $LogData = $model->where("id > $maxValue")->limit($limit)->order("id ASC")->select();
        } catch (Exception  $e) {
            Log::error($e->getMessage()."data====>".json_encode($data,true));
            return $data;
        }

        $returnData = [];
        foreach ($LogData as $row) {
            $row["platform"] = PLATFORM;
            $row["single_id"] = $row["id"];
            $row["plat_server"] = PLAT_SERVER_ID;
            $row["master_platform"] = PLATFORM;
            $row["tower_id"] = $row["max_tower_id"];
            
            // $row["player_id"] = $row["role_id"];
            // $row["pay_month"] = date("Ym", $row["pay_time"]);
            unset($row["id"]);
            unset($row["max_tower_id"]);
            $returnData[] = $row;
        }

        $data["data"] = $returnData;
        return $data;
    }
}